<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\CaseCustomer;

class AddBatchCountIntoCaseCustomers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('case_customers',function (Blueprint $table) {
            $table->string('batch_count')->after('fields')->nullable();
        });
        $customer = CaseCustomer::withCount('caseBatchs')->get();
        foreach ($customer as $item) {
            $item->batch_count = $item->case_batchs_count;
            $item->save();
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('case_customers', function (Blueprint $table) {
            $table->dropColumn('batch_count');
        });
    }
}
